<?
 $title = "Status pages";
 $dir = "../";
 include($dir."inc/header.inc.php");
 require("sks.inc.php");
 require("sks-status.inc.php");
 
 $servers = unserialize(file_get_contents(dirname(__FILE__)."/sks_cache.serialized"));
 $status_collection = unserialize(file_get_contents(dirname(__FILE__)."/sks_cache_status_collection.serialized"));
 
 echo "<p>These statistics were last updated: ".date("Y-m-d H:i",$servers->get_time())." (CET)</p>";
 $serverarr = $servers->get_servers();
 $servercolarr = $status_collection->get_servers();
 sort($serverarr);
 
 $numkey=0;
 $ks=array();
 foreach($serverarr as $serid=>$server)
 {
  $ks[$server->get_hostname()]=$serid;
  
  if($server->get_hostname()=="keys.kfwebs.net")
  {
   $numkey=$server->get_numkeys();
  }
 }
 if(isset($ks[$_GET['server']]))
 {
  $id = $ks[$_GET['server']];
  ?>
   <h1>Status for <?=$serverarr[$id]->get_hostname();?></h1>
   <table class="list">
    <tr>
     <td>Version</td>
     <td><?=$serverarr[$id]->get_version();?></td>
    </tr>
    <tr>
     <td>IPv6</td>
     <td><?=(($serverarr[$id]->get_ipv6()==1) ? "Yes" : "No");?></td>
    </tr>
    <tr>
     <td>Keys</td>
     <td><?=$serverarr[$id]->get_numkeys();?></td>
    </tr>
    <tr>
     <td>SRV Weight</td>
     <td><?=$serverarr[$id]->get_srvweight();?></td>
    </tr>
    <tr>
     <td>Status page</td>
     <td><a href="http://<?=$serverarr[$id]->get_hostname();?>:11371/pks/lookup?op=stats">Status</a></td>
    </tr>
    <tr>
     <td>Latest status</td>
     <td><?=($servercolarr[$serverarr[$id]->get_hostname()]['last_status']==1) ? "OK" : "Not OK";?></td>
    </tr>
    <?
     if($servercolarr[$serverarr[$id]->get_hostname()]['last_status']!=1)
     {
      echo "<tr><td>Reason</td><td>{$servercolarr[$serverarr[$id]->get_hostname()]['last_status_reason']}</td></tr>";
     }
    ?>
    <tr>
     <td>Last status change</td>
     <td><?=date("Y-m-d", $servercolarr[$serverarr[$id]->get_hostname()]['last_status_change']);?></td>
    </tr>
   </table>
   <h1>Recorded peers</h1>
   <table class="list">
   <tr>
    <th>Name</th>
    <th>Latest status</th>
    <th>Latest status changed</th>
    <th>Number of keys</th>
    <th>Cross-peered</th>
   </tr>
   <?
    $peers = $serverarr[$id]->get_peers();
    
    foreach($peers as $pks)
    {
     echo "<tr>";
     echo "<td><a href=\"/status/info/{$pks[0]}\">".$pks[0]."</a></td>";
     echo "<td>".(($servercolarr[$pks[0]]['last_status']==1) ? "OK": "Not OK")."</td>";
     echo "<td>".(isset($servercolarr[$pks[0]]['last_status_change']) ? date("Y-m-d", $servercolarr[$pks[0]]['last_status_change']) : "not set")."</td>";
     echo "<td>".(isset($serverarr[$ks[$pks[0]]]) ? $serverarr[$ks[$pks[0]]]->get_numkeys() : "")."</td>";
     echo "<td>";
     
     if(isset($serverarr[$ks[$pks[0]]]) && $serverarr[$ks[$pks[0]]]->get_statusok()==1)
     {
      $f=0;
      foreach($serverarr[$ks[$pks[0]]]->get_peers() as $v)
      {
       if($v[0]==$serverarr[$id]->get_hostname())
       {
        $f=1;
        break;
       }
      }
      if($f==1) echo "OK";
      else echo "Not OK";
     }
     else
     {
      echo "Unknown";
     }
     echo "</td>";
     echo "</tr>";
    }
   ?>
   </table>
  <?  
  
  
 }
 else
 {
  echo "<p>No data found for keyserver</p>";
 }
 /*
 foreach($serverarr as $server)
 {
  if($server->get_statusok()!=1 || $server->get_numkeys() < ($numkey-500) || $server->get_hostname()=="localhost")
  {
   $nonrespserver[] = array($server->get_hostname(),$server->get_port(),$server->get_statusok(),$server->get_version(),$server->get_numkeys());
  }
  */
 
 include($dir."inc/footer.inc.php");
?>
